package oa.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.alibaba.fastjson.JSON;

import oa.dateverify.DateTimeVerify;
import oa.entity.Attendance;
import oa.entity.User;
import oa.service.impl.AttendanceServiceImpl;
import oa.service.impl.UserServiceImpl;

/**
 * Servlet implementation class Attendance
 */
@WebServlet("/showattendance")
public class AttendanceServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public AttendanceServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("UTF-8");
		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/html;charset=utf-8");
		HttpSession session = request.getSession();
		PrintWriter out = response.getWriter();
		UserServiceImpl user = new UserServiceImpl();
		AttendanceServiceImpl attendan = new AttendanceServiceImpl();
		Attendance att = new Attendance();
		DateTimeVerify IsVerify = new DateTimeVerify(); // 实例化时间验证类
		String opt = request.getParameter("opt");
		int userid = Integer.parseInt(request.getParameter("userid"));
		boolean flag = false;
		boolean Verify = false; // 验证当前签到的时间
		Map<String, Object> maplist = new HashMap<String, Object>(2);
		List<User> userlist = new ArrayList<User>();
		List<Attendance> attendancelist = new ArrayList<Attendance>();
		List<Attendance> attendancelistone = new ArrayList<Attendance>();
		List<Attendance> attendanceqiandaolist = new ArrayList<Attendance>();

		// 判断签到
		if ("qiandao".equals(opt)) {
			try {
				SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
				Date date1 = sf1.parse(sf1.format(new Date()));
				att.setAttendanceStarttime(date1);// 签到时间
				att.setUserId(userid); // 员工id
				attendanceqiandaolist = attendan.selectTimeUserid(att);
				if (attendanceqiandaolist.size() == 0) {// 判断是否签到
					userlist = user.selectUser(userid, null, 0, 0);
					int departid = 0;// 部门
					int companyid = 0;// 公司
					SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = sf.parse(sf.format(new Date()));
					Verify = IsVerify.isInDate(date, "07:00:00", "09:00:00");
					if (Verify == true) {
						maplist.put("Verify", Verify);
						for (User it : userlist) {
							departid = it.getDeptId();
							companyid = it.getCompanyId();
						}
						att.setAttendanceStarttime(date);// 签到时间
						att.setUserId(userid); // 员工id
						att.setDeptId(departid); // 部门id
						att.setCompanyId(companyid); // 公司id
						flag = attendan.addAttendance(att);
						maplist.put("bool", flag);
					} else if (Verify == false) {
						maplist.put("Verify", Verify);
					}
				} else if (attendanceqiandaolist.size() != 0) {
					maplist.put("bool", flag);

				}

			} catch (Exception e) {

			}
			out.print(JSON.toJSONString(maplist));
			// 判断签退
		} else if ("qiantui".equals(opt)) {
			int attendanceid = 0; // 定义考勤ID
			try {
				SimpleDateFormat sf1 = new SimpleDateFormat("yyyy-MM-dd");
				Date date1 = sf1.parse(sf1.format(new Date()));
				att.setAttendanceStarttime(date1);// 签到时间
				att.setUserId(userid); // 员工id
				attendancelist = attendan.selectTimeUserid(att);
				if (attendancelist.size() != 0) { // 通过签到时间和员工id判断数据库是否存在数据
					for (Attendance attendanceqiandao : attendancelist) {
						attendanceid = attendanceqiandao.getAttendanceId();
					}
					att.setAttendanceStarttime(null);// 签到时间
					att.setAttendanceEndtime(date1);// 签退时间
					att.setUserId(userid); // 员工id
				}

				attendancelistone = attendan.selectTimeUserid(att);
				if (attendancelistone.size() == 0) { // 判断该员工今天是否签退
					SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
					Date date = sf.parse(sf.format(new Date()));
					Verify = IsVerify.isInDate(date, "17:30:00", "19:00:00");
					if (Verify == true) { //判断当前时间是否在签退时间范围
						maplist.put("Verify", Verify);
						att.setUserId(userid); // 签到员工
						att.setAttendanceEndtime(date); // 签退时间
						att.setAttendanceId(attendanceid); // 考勤id
						flag = attendan.updateAttendance(att);
						maplist.put("bool", flag);

					} else if (Verify == false) {  //否则就是false
						maplist.put("Verify", Verify);
					}

				} else{ //如果用户已经签到过
					maplist.put("bool", flag);

				}
				out.print(JSON.toJSONString(maplist));
				
			} catch (Exception e) {

			}
			
		}
		out.flush();
		out.close();

	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doGet(request, response);
	}

}
